//
//  YRControlButton.m
//  YouRong4iPhone
//
//  Created by 张常兴 on 15/3/22.
//  Copyright © 2015年 小怪兽. All rights reserved.
//

#import "YRControlButton.h"

#define SEPARATOR_WIDTH                     1.f
#define YR_CHANNEL_ANIMATION_DURATION       .2f
#define _SEPARTOR_LEFT                      12.f
#define _SEPARTOR_WIDTH                     25.f
#define _SEPARTOR_HEIGHT                    2.f

@interface YRControlButton () {
    BOOL            _isHome;
    NSMutableArray  *_iconImageArray;
    NSInteger       _separtorLeft;
    UIColor         *_buttonBackgroundColor;
    UIColor         *_buttonSelectedColor;
    UILabel         *_countLabel;
    NSInteger       _count;
    UIView          *_selectedColor;
    UIImageView     *_selectedImg;
    NSInteger       _buttonWidth;
    NSMutableArray  *_iconArray;
    NSMutableArray  *_selectedIconArray;
}

@property (nonatomic, assign) NSInteger        selectedIndex;
@property (nonatomic, strong) NSMutableArray   *btnArray;
@property (nonatomic, strong) UIColor          *selectedTextColor;
@property (nonatomic, strong) UIColor          *normalTextColor;

@end

@implementation YRControlButton

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _isHome = NO;
        _selectedIndex = 0;
        _separtorLeft = _SEPARTOR_LEFT;
        _buttonBackgroundColor = WHITE_COLOR;
        _buttonSelectedColor = UIColorFromRGB(0xF6F6F6);
        _selectedTextColor = STYLE_COLOR;
        _normalTextColor = BLACK_TEXT_TITLE;
        self.backgroundColor = _buttonBackgroundColor;
    }
    return self;
}


- (void)initButtonWithTitles:(NSArray *)titles
                        icon:(NSArray *)icons
                iconSelected:(NSArray *)selectedIcons
             isShowSeparator:(BOOL     )separator
             isShortSelector:(BOOL     )isShort
         setButtonBackground:(UIColor *)color
 setButtonSelectedBackground:(UIColor *)selectedColor
                defaultIndex:(NSInteger)index {
    _selectedIndex = index;
    _count = titles.count;
    [self removeAllSubviews];
    
    [self setButtonBackground:color];
    [self setButtonSelectedBackground:selectedColor];
    
    
    
    UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.height - 5)];
    bg.backgroundColor = _buttonBackgroundColor;
    [self addSubview:bg];
    
    NSInteger num = [titles count];
    
    _btnArray = [[NSMutableArray alloc] initWithCapacity:num];
    _iconArray = [icons mutableCopy];
    _selectedIconArray = [selectedIcons mutableCopy];
    NSInteger i = 0;
    
    _buttonWidth = (self.width - (num - 1) * SEPARATOR_WIDTH) / num;
    
    for (NSString *title in titles) {
        //按钮
        UIImage *icon = nil;
        if (icons) {
            icon = [icons objectAtIndex:i];
        }
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.opaque = NO;
        btn.frame = CGRectMake((_buttonWidth + SEPARATOR_WIDTH ) * i, 0, _buttonWidth, self.height);
        if (icon) {
            [self  buttonWithImage:icon imageEdgeInsets:UIEdgeInsetsMake(self.height/2- icon.size.height/2-2, _buttonWidth/8, self.height/2- icon.size.height/2-2, _buttonWidth/8 + icon.size.width) title:title titleEdgeInsets:UIEdgeInsetsMake(self.height/2- icon.size.height/2-2, _buttonWidth/8 , self.height/2- icon.size.height/2-2, 0) font:FONT(16) target:self action:@selector(buttonAction:) button:btn];
        } else {
            [self buttonWithImage:nil imageEdgeInsets:UIEdgeInsetsZero title:title titleEdgeInsets:UIEdgeInsetsZero font:FONT(16) target:self action:@selector(buttonAction:) button:btn];
        }
        
        if (i == _selectedIndex) {
            if (icons) {
                [btn setImage:_selectedIconArray[i] forState:UIControlStateNormal];
                [btn setImage:_selectedIconArray[i] forState:UIControlStateHighlighted];
            }
            [btn setTitleColor:_selectedTextColor forState:UIControlStateNormal];
            [btn setBackgroundColor:_buttonSelectedColor];
        }
        btn.tag = i;
        [self addSubview:btn];
        [_btnArray addObject:btn];
        
        //分割线
        if (i < num - 1 && separator) {
            UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(btn.right,10, SEPARATOR_WIDTH, self.height-20)];
            separator.backgroundColor = UIColorFromRGB(0xE2E2E2);
            [self addSubview:separator];
        }
        i ++;
    }
    
    UIView *bottomSeparator = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 1, self.width, 1)];
    bottomSeparator.backgroundColor = UIColorFromRGB(0xE2E2E2);
    [self addSubview:bottomSeparator];
    
    //颜色条
//    _selectedColor = [[UIView alloc] initWithFrame:CGRectMake((_buttonWidth + SEPARATOR_WIDTH)* _selectedIndex  + _SEPARTOR_LEFT,self.height - 1, _buttonWidth - _SEPARTOR_WIDTH, _SEPARTOR_HEIGHT)];
//    _selectedColor.backgroundColor = _selectedTextColor;
//    [self addSubview:_selectedColor];
    
//        UIImageView *shadowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"controlbutton_shadow"]];
//        shadowView.top = self.height;
//        [self addSubview:shadowView];
    
    [self isShortSelector:isShort];
}


- (void)isShortSelector:(BOOL)isShort {
    if (!isShort)
        _selectedColor.frame = CGRectMake((_buttonWidth + SEPARATOR_WIDTH)* _selectedIndex ,self.height - _SEPARTOR_HEIGHT, _buttonWidth, _SEPARTOR_HEIGHT);
        _separtorLeft = 0;
}

- (void)setButtonBackground:(UIColor *)color {
    if (color)
        _buttonBackgroundColor = color;
}

- (void)setButtonSelectedBackground:(UIColor *)color {
    if (color)
        _buttonSelectedColor = color;
}

- (void)buttonAction:(UIButton *)btn {
    if (btn.tag == _selectedIndex)
        return;
    
    [self selectAtIndex:btn.tag];
    if (self.block)
        self.block(btn.tag);
}

- (void)selectAtIndex:(NSInteger)index {
   ((UIButton*)(_btnArray[_selectedIndex])).titleLabel.font = FONT(16);
    if ([_iconArray count] > 0) {
        if ([_iconArray[_selectedIndex] isKindOfClass:[UIImage class]]) {
            ((UIImageView*)(_iconImageArray[_selectedIndex])).image = _iconArray[_selectedIndex];
        }
    }
    
    if (_isHome) {
        [(UIButton*)(_btnArray[_selectedIndex]) setTitleColor:_normalTextColor  forState:UIControlStateNormal];
    } else {
        [(UIButton*)(_btnArray[_selectedIndex]) setImage:_iconArray[_selectedIndex] forState:UIControlStateNormal];
        [(UIButton*)(_btnArray[_selectedIndex]) setImage:_iconArray[_selectedIndex] forState:UIControlStateHighlighted];
        [(UIButton*)(_btnArray[_selectedIndex]) setBackgroundColor:_buttonBackgroundColor];
        [(UIButton*)(_btnArray[index]) setBackgroundColor:_buttonSelectedColor];
        [(UIButton*)(_btnArray[_selectedIndex]) setTitleColor:_normalTextColor  forState:UIControlStateNormal];
    }
    _selectedIndex = index;
    
    [UIView animateWithDuration:YR_CHANNEL_ANIMATION_DURATION animations:^{
        _selectedColor.left = (_buttonWidth + SEPARATOR_WIDTH)* _selectedIndex + _separtorLeft;//_SEPARTOR_LEFT;
    }];
    
    if (_selectedImg) {
        _selectedImg.left = (_buttonWidth + SEPARATOR_WIDTH ) * _selectedIndex;
    }

    if ([_selectedIconArray count] > 0) {
        if ([_selectedIconArray[_selectedIndex] isKindOfClass:[UIImage class]]) {
            ((UIImageView*)(_iconImageArray[_selectedIndex])).image = _selectedIconArray[_selectedIndex];
            if (!_isHome) {
                [(UIButton*)(_btnArray[_selectedIndex]) setImage:_selectedIconArray[_selectedIndex] forState:UIControlStateNormal];
                [(UIButton*)(_btnArray[_selectedIndex]) setImage:_selectedIconArray[_selectedIndex] forState:UIControlStateHighlighted];
            }
        }
    }
    [(UIButton*)(_btnArray[_selectedIndex]) setTitleColor:_selectedTextColor  forState:UIControlStateNormal];
}

- (void)setBoldFontAtIndex:(NSInteger)index {
    ((UIButton*)(_btnArray[index])).titleLabel.font = BOLD_FONT(16);
}

- (void)clear {
    self.block = nil;
    [self selectAtIndex:0];
}


- (void)buttonWithImage:(UIImage *)image
        imageEdgeInsets:(UIEdgeInsets)imageEdgeInsets
                  title:(NSString *)title
        titleEdgeInsets:(UIEdgeInsets)titleEdgeInsets
                   font:(UIFont *)font
                 target:(id)target
                 action:(SEL)action
                 button:(UIButton*)button {
    button.backgroundColor = _buttonBackgroundColor;
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:image forState:UIControlStateHighlighted];
    button.imageEdgeInsets = imageEdgeInsets;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:_normalTextColor forState:UIControlStateNormal];
    button.titleLabel.font = font;
    button.titleEdgeInsets = titleEdgeInsets;
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
}


@end